package com.yanqu.road.dao.impl.activity.eggs;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.eggs.*;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class EggsUserDataDaoImpl extends TempDataObjectDao<EggsUserData> {

    public EggsUserData getEggsUserData(int activityId, long userId) {
        String sql = "select * from `t_u_activity_eggs` where `activityId`=? and `userId`=?";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, activityId);
            call.setLong(2, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean updateData(EggsUserData data) {
        String sql = "update `t_u_activity_eggs` set curTestId=?,isTestReward=?,sceneEventNo=?,classRoomData=?,testData=?,songData=?," +
                "storyData=? where activityId=? and userId=?";
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurTestId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isTestReward()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSceneEventNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getClassRoomDataMapJsonStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getTestDataMapJsonStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getSongDataMapJsonStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getStoryDataMapJsonStr()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean addData(EggsUserData data) {
        String sql = "insert into `t_u_activity_eggs`(activityId,userId,curTestId,isTestReward,sceneEventNo,classRoomData,testData," +
                "songData,storyData) values (?,?,?,?,?,?,?,?,?)";
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCurTestId()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isTestReward()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSceneEventNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getClassRoomDataMapJsonStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getTestDataMapJsonStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getSongDataMapJsonStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getStoryDataMapJsonStr()));
        if (getDb().executeNoneQuery(sql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected EggsUserData parseData(ResultSet rs) throws SQLException {
        EggsUserData data = new EggsUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setCurTestId(rs.getInt("curTestId"));
        data.setTestReward(rs.getBoolean("isTestReward"));
        data.setSceneEventNo(rs.getInt("sceneEventNo"));
        String value = rs.getString("classRoomData");
        if(!StringUtils.isNullOrEmpty(value)){
            Map<Integer, EggsClassRoomData> classRoomDataMap = JSON.parseObject(value, new TypeReference<Map<Integer, EggsClassRoomData>>() {});
            data.setClassRoomDataMap(classRoomDataMap);
        }
        value = rs.getString("testData");
        if(!StringUtils.isNullOrEmpty(value)){
            Map<Integer, EggsTestData> testDataMap = JSON.parseObject(value, new TypeReference<Map<Integer, EggsTestData>>() {});
            data.setTestDataMap(testDataMap);
        }
        value = rs.getString("songData");
        if(!StringUtils.isNullOrEmpty(value)){
            Map<Integer, EggsSongData> songDataMap = JSON.parseObject(value, new TypeReference<Map<Integer, EggsSongData>>() {});
            data.setSongDataMap(songDataMap);
        }
        value = rs.getString("storyData");
        if(!StringUtils.isNullOrEmpty(value)){
            Map<Integer, EggsStoryData> storyDataMap = JSON.parseObject(value, new TypeReference<Map<Integer, EggsStoryData>>() {});
            data.setStoryDataMap(storyDataMap);
        }
        return data;
    }
}
